
import path from "path";
import ExcelJS from 'exceljs';
import { FileUtils, ExcelReporter } from '../../utils/ReadAndWritingUtils'
import { StringConstant } from '../../utils/Constant'
import { ComplieFileInfo, OutPutInfo } from '../../typedef/parseLog/parseLog'
import { compileFileInfo } from './compileFilePerformance'
import { applicationInfoMap } from "../../utils/functionUtils";


export class SDKComparison {

  /**
   * 读文件夹中log文件，返回SDK编译应用耗时对比结果与告警差异对比结果
   *
   * @param oldFilePath old complie log path
   * @param newFilePath new complie log path
   * @returns
   */
  static ComparisonResultsOfSDK(oldFilePath: string, newFilePath: string): Array<OutPutInfo> {
    const oldFileList: Array<string> = FileUtils.readFilesInDir(oldFilePath, (name) => {
      return name.endsWith(StringConstant.LOG);
    });
    const newFileList: Array<string> = FileUtils.readFilesInDir(newFilePath, (name) => {
      return name.endsWith(StringConstant.LOG);
    });

    const oldCompile: Map<string, ComplieFileInfo> = compileFileInfo.compileFileContent(oldFileList);
    const newCompile: Map<string, ComplieFileInfo> = compileFileInfo.compileFileContent(newFileList);
    const outList: Array<OutPutInfo> = compileFileInfo.compileMap(oldCompile, newCompile)

    return outList;
  }
}


/**
 * 输出SDK编译应用比对的Excel文件
 * 
 * @param oldDirPath 旧版文件夹路径
 * @param newDirPath 新版文件夹路径
 * @param outputExcelPath 输出的Excel文件路径
 */
export function main(oldDirPath: string, newDirPath: string,  workbook: ExcelJS.Workbook): void {
  const list: Array<OutPutInfo> = SDKComparison.ComparisonResultsOfSDK(path.resolve(oldDirPath), path.resolve(newDirPath));
  ExcelReporter(
    list,
    workbook
  );
}
